<?php
	require_once('config/mind.php');
	require_once(''.$_MIND['framework']);
	require_once(''.$_MIND['header']);
	
	
	$current= $_MIND['fw']->loadXML($_MIND['rootDir'].'config/current.xml');
	$localCurrent= $_MIND['rootDir'].'config/current.xml';
	$localCurrentDir= $localCurrent;
	
	if(!@$serverCurrent= @file_get_contents($_MIND['updateSource']))
	{
		?>
				<div style='border:solid 1px #666;padding:4px;margin:8px;text-align:center; background-color:#f99;color:#fff;font-weight:bold;'>
					The server couldn't be reached. The system could not search for updates.
				</div>
		<?php
		exit;
	}
	//$serverCurrent= @file_get_contents($_MIND['updateSource']);
	
	$localCurrent= @simplexml_load_file($localCurrent);
	$serverCurrent= @simplexml_load_string($serverCurrent);
	
	$hereVs= explode('.', (string)$localCurrent->mindFiles['version']);
	$curVs= explode('.', (string)$serverCurrent->mindFiles['version']);
	$updated= false;
	
	$changedFiles= Array();
	
	if((string)$localCurrent->mindFiles['version'] != (string)$serverCurrent->mindFiles['version'])
	{
		foreach($serverCurrent->mindFiles->file as $file)
		{
			$continue= false;
			if(!file_exists('../'. $file['addr']))
			{
				$continue= true;
			}else{
					$match= (array)$localCurrent->xpath("mindFiles/file[@addr='".$file['addr']."']");
					
					if(isset($match[0]))
					{
						$match= $match[0];
						if((integer)$match['version'] < (integer)$file['version'])
							$continue= true;
					}else{
							$continue= true;
						 }
				 }
				 
			//$file['addr']= '../'.$file['addr'];
			$curPath= '';
			if($continue)
			{
				$updated= true;
				$addr= explode('/', (string)$file['addr']);
				$curPath= '';
				for($i=0, $j= sizeof($addr)-1; $i<$j; $i++)
				{
					if(!file_exists('../'.$curPath.$addr[$i]))
					{
						mkdir('../'.$curPath.$addr[$i]);
					}
					$curPath.= $addr[$i].'/';
				}
				fclose(fopen('../'.$curPath.$addr[$i], 'w+'));
				file_put_contents('../'.$curPath.$addr[$i],
								  file_get_contents(preg_replace('/current\.xml$/', '', $_MIND['updateSource']).'/index.php?update='.$curPath.$addr[$i]));
				$changedFiles[]= $addr[$i];
				$localCurrent->details= $serverCurrent->details;
			}
		}
	}else{
			$updated= false;
		 }
	if($updated)
	{
		$localCurrent->data->lastUpdate['value']= time();
		$localCurrent->mindFiles= $serverCurrent->mindFiles;
		
		for($i=0, $j=sizeof($serverCurrent->mindFiles->file); $i<$j; $i++)
		{
			$localCurrent->mindFiles->addChild('file');
			$localCurrent->mindFiles->file[$i]['addr']= (string)$serverCurrent->mindFiles->file[$i]['addr'];
			$localCurrent->mindFiles->file[$i]['version']= (string)$serverCurrent->mindFiles->file[$i]['version'];
		}
		
		if(!isset($opts))
			$opts= $_MIND['fw']->loadOptions();
		
		if(
			((integer)$hereVs[0] < (integer)$curVs[0] && (integer)$opts['actionWithNewVersion']!= 2)
			||
			((integer)$hereVs[1] < (integer)$curVs[1] && (integer)$opts['actionWithNewSubVersion']!= 2)
			||
			((integer)$hereVs[2] < (integer)$curVs[2] && (integer)$opts['actionWithNewUpdates']!= 2)
		  )
		{
			//echo "<img src='".$_MIND['imageDir']."/visto.png'>";
			?>
				<div style='border:solid 1px #666;padding:20px;margin:8px; background-color:#cfc;color:#393;font-weight:bold;'>
					<img src='<?php echo $_MIND['imageDir'].'/'; ?>visto.png' />
					theWebMind 2.0 has just been updated to its latest version, <?php echo implode('.', $curVs); ?>
				</div>
					<fieldset>
						<legend>
							Details
						</legend>
						<pre style='text-align:left;'>
						<?php
							echo (string)$serverCurrent->details;
						?>
						</pre>
					</fieldset>
					<div style='tet-align:left;
								height:100px;
								overflow:auto;'>
						Changed files:<br/>
						<?php
							for($i=0, $j=sizeof($changedFiles); $i<$j;$i++)
							{
								echo $changedFiles[$i].'<br/>';
							}
						?>
					</div>
				</div>
			<?php
		}
	}else{
			//echo "<img src='".$_MIND['imageDir']."/visto.png'>Your system is already in the lates version.";
			?>
			<div style='border:solid 1px #666;padding:20px;margin:8px; background-color:#cfc;color:#393;font-weight:bold;'>
					<img src='<?php echo $_MIND['imageDir'].'/'; ?>visto.png' />
					Your system is already using the latest version
			</div>
			<?php
		 }
	$localCurrent->mindFiles['version']= (string)$serverCurrent->mindFiles['version'];
	$localCurrent->data->lastCheck['value']= time();
	$_MIND['fw']->saveXML($localCurrent, $localCurrentDir);
	
?>
